package com.huike.report.service.impl;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbActivity;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.clues.mapper.*;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.contract.domain.TbContract;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.*;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IReportService;
import lombok.SneakyThrows;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;

@Service
public class ReportServiceImpl implements IReportService {

    @Autowired
    private TbContractMapper contractMapper;


    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbActivityMapper activityMapper;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;

    @Autowired
    private ReportMapper reportMpper;

    @Override
    public LineChartVO contractStatistics(String beginCreateTime, String endCreateTime) {
        LineChartVO lineChartVo = new LineChartVO();
        try {
            List<String> timeList = findDates(beginCreateTime, endCreateTime);
            lineChartVo.setxAxis(timeList);
            List<LineSeriesVO> series = new ArrayList<>();
            List<Map<String, Object>> statistics = contractMapper.contractStatistics(beginCreateTime, endCreateTime);
            LineSeriesVO lineSeriesDTO1 = new LineSeriesVO();
            lineSeriesDTO1.setName("新增客户数");
            LineSeriesVO lineSeriesDTO2 = new LineSeriesVO();
            lineSeriesDTO2.setName("客户总数");
            int sum = 0;
            for (String s : timeList) {
                Optional optional = statistics.stream().filter(d -> d.get("dd").equals(s)).findFirst();
                if (optional.isPresent()) {
                    Map<String, Object> cuurentData = (Map<String, Object>) optional.get();
                    lineSeriesDTO1.getData().add(cuurentData.get("num"));
                    sum += Integer.parseInt(cuurentData.get("num").toString());
                } else {
                    lineSeriesDTO1.getData().add(0);
                }
                lineSeriesDTO2.getData().add(sum);
            }
            series.add(lineSeriesDTO1);
            series.add(lineSeriesDTO2);
            lineChartVo.setSeries(series);
        } catch (ParseException e) {
            // e.printStackTrace();
        }
        return lineChartVo;
    }

    @Override
    public LineChartVO salesStatistics(String beginCreateTime, String endCreateTime) {
        LineChartVO lineChartVo = new LineChartVO();
        try {
            List<String> timeList = findDates(beginCreateTime, endCreateTime);
            lineChartVo.setxAxis(timeList);
            List<LineSeriesVO> series = new ArrayList<>();
            List<Map<String, Object>> statistics = contractMapper.salesStatistics(beginCreateTime, endCreateTime);
            LineSeriesVO lineSeriesVo = new LineSeriesVO();
            lineSeriesVo.setName("销售统计");
            int sum = 0;
            for (String s : timeList) {
                Optional optional = statistics.stream().filter(d -> d.get("dd").equals(s)).findFirst();
                if (optional.isPresent()) {
                    Map<String, Object> cuurentData = (Map<String, Object>) optional.get();
                    lineSeriesVo.getData().add(cuurentData.get("sales"));
                } else {
                    lineSeriesVo.getData().add(0);
                }
            }
            series.add(lineSeriesVo);
            lineChartVo.setSeries(series);
        } catch (ParseException e) {
            // e.printStackTrace();
        }
        return lineChartVo;
    }


    /**
     * 渠道统计
     */
    @Override
    public List<Map<String, Object>> chanelStatistics(String beginCreateTime, String endCreateTime) {
        List<Map<String, Object>> data = contractMapper.chanelStatistics(beginCreateTime, endCreateTime);
        for (Map<String, Object> datum : data) {
            String subjectValue = (String) datum.get("channel");
            String lable = sysDictDataMapper.selectDictLabel("clues_item", subjectValue);
            datum.put("channel", lable);
        }
        return data;
    }

    @Override
    public List<TbContract> contractReportList(TbContract tbContract) {
        return contractMapper.selectTbContractList(tbContract);
    }


    @Override
    public List<Map<String, Object>> activityStatistics(String beginCreateTime, String endCreateTime) {
        List<Map<String, Object>> data = contractMapper.activityStatistics(beginCreateTime, endCreateTime);
        for (Map<String, Object> datum : data) {
            Long activityId = (Long) datum.get("activity_id");
            TbActivity tbActivity = activityMapper.selectTbActivityById(activityId);
            if (tbActivity == null) {
                datum.put("activity", "其他");
            } else {
                datum.put("activity", tbActivity.getName());
            }
        }
        return data;
    }

    /**
     * 按照部门统计销售
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public List<Map<String, Object>> deptStatisticsList(String beginCreateTime, String endCreateTime) {
        List<Map<String, Object>> data = contractMapper.deptStatistics(beginCreateTime, endCreateTime);
        for (Map<String, Object> datum : data) {
            Long deptId = (Long) datum.get("dept_id");
            if (deptId != null) {
                SysDept dept = deptMapper.selectDeptById(deptId);
                datum.put("deptName", dept.getDeptName());
            }
        }
        return data;
    }


    /**
     * 按照渠道统计销售
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public List<Map<String, Object>> channelStatisticsList(String beginCreateTime, String endCreateTime) {
        List<Map<String, Object>> data = contractMapper.channelStatistics(beginCreateTime, endCreateTime);
        for (Map<String, Object> datum : data) {
            String subjectValue = (String) datum.get("channel");
            if (subjectValue != null) {
                String lable = sysDictDataMapper.selectDictLabel("clues_item", subjectValue);
                datum.put("channel", lable);
            }
        }
        return data;
    }


    /**
     * 按照归属人统计销售
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public List<Map<String, Object>> ownerShipStatisticsList(String beginCreateTime, String endCreateTime) {
        return contractMapper.ownerShipStatistics(beginCreateTime, endCreateTime);
    }


    @Override
    public List<TbClue> cluesStatisticsList(TbClue clue) {
        return clueMapper.selectTbClueForReport(clue);
    }

    @Override
    public List<ActivityStatisticsVo> activityStatisticsList(TbActivity query) {
        query.setStatus("2");
        List<TbActivity> activities = activityMapper.selectTbActivityList(query);
        Map<String, Object> timeMap = query.getParams();
        List<ActivityStatisticsVo> list = new ArrayList<>();
        for (TbActivity activity : activities) {
            ActivityStatisticsVo dto = new ActivityStatisticsVo();
            BeanUtils.copyProperties(activity, dto);
            TbClue tbClue = new TbClue();
            tbClue.setActivityId(activity.getId());
            tbClue.setChannel(activity.getChannel());
            tbClue.setParams(timeMap);
            Map<String, Object> clueCount = clueMapper.countByActivity(tbClue);
            if (clueCount != null) {
                dto.setCluesNum(Integer.parseInt(clueCount.get("total").toString()));
                if (clueCount.get("falseClues") != null) {
                    dto.setFalseCluesNum(Integer.parseInt(clueCount.get("falseClues").toString()));
                }
                if (clueCount.get("toBusiness") != null) {
                    dto.setBusinessNum(Integer.parseInt(clueCount.get("toBusiness").toString()));
                }
            }
            TbContract tbContract = new TbContract();
            tbContract.setChannel(activity.getChannel());
            tbContract.setActivityId(activity.getId());
            tbContract.setParams(timeMap);
            Map<String, Object> contractCount = contractMapper.countByActivity(tbContract);
            if (contractCount != null) {
                dto.setCustomersNum(Integer.parseInt(contractCount.get("customersNum").toString()));
                if (contractCount.get("amount") == null) {
                    dto.setAmount(0d);
                } else {
                    dto.setAmount((Double) contractCount.get("amount"));
                }
                if (contractCount.get("cost") == null) {
                    dto.setCost(0d);
                } else {
                    dto.setCost((Double) contractCount.get("cost"));
                }

            }
            list.add(dto);
        }
        return list;
    }

    /**
     * *************看我看我**************
     * 传入两个时间范围，返回这两个时间范围内的所有时间，并保存在一个集合中
     *
     * @param beginTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static List<String> findDates(String beginTime, String endTime)
            throws ParseException {
        List<String> allDate = new ArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date dBegin = sdf.parse(beginTime);
        Date dEnd = sdf.parse(endTime);
        allDate.add(sdf.format(dBegin));
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            allDate.add(sdf.format(calBegin.getTime()));
        }
        System.out.println("时间==" + allDate);
        return allDate;
    }


    @Override
    public IndexVo getIndex(IndexStatisticsVo request) {
        Long deptId = request.getDeptId();
        TbAssignRecord tbAssignRecord = new TbAssignRecord();
        tbAssignRecord.setLatest("1");
        assignRecordMapper.selectAssignRecordList(tbAssignRecord);
        return null;
    }

    @Override
    public List<Map<String, Object>> salesStatisticsForIndex(IndexStatisticsVo request) {
        List<Map<String, Object>> list = contractMapper.contractStatisticsByUser(request);
        for (Map<String, Object> datum : list) {
            Long deptId = (Long) datum.get("dept_id");
            if (deptId != null) {
                SysDept dept = deptMapper.selectDeptById(deptId);
                datum.put("deptName", dept.getDeptName());
            }
        }
        return list;
    }


    /**
     * ************看我看我***********
     * 用我能少走很多路
     * 我是用来机选百分比的方法
     *
     * @param all
     * @param num
     * @return
     */
    private BigDecimal getRadio(Integer all, Long num) {
        if (all.intValue() == 0) {
            return new BigDecimal(0);
        }
        BigDecimal numBigDecimal = new BigDecimal(num);
        BigDecimal allBigDecimal = new BigDecimal(all);
        BigDecimal divide = numBigDecimal.divide(allBigDecimal, 4, BigDecimal.ROUND_HALF_UP);
        return divide.multiply(new BigDecimal(100));
    }


    /**
     * 获取首页基本数据
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public IndexBaseInfoVO getBaseInfo(String beginCreateTime, String endCreateTime) {
        //1）构建一个空的结果集对象
        IndexBaseInfoVO result = new IndexBaseInfoVO();
        //2 封装结果集属性
        // 2.1 由于查询需要用到用户名 调用工具类获取用户名
        String username = SecurityUtils.getUsername();
        try {
            //3 封装结果集对象
            result.setCluesNum(reportMpper.getCluesNum(beginCreateTime, endCreateTime, username));
            result.setBusinessNum(reportMpper.getBusinessNum(beginCreateTime, endCreateTime, username));
            result.setContractNum(reportMpper.getContractNum(beginCreateTime, endCreateTime, username));
            result.setSalesAmount(reportMpper.getSalesAmount(beginCreateTime, endCreateTime, username));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        //4 返回结果集对象
        return result;
    }

    /**
     * 查询当前用户的基本数据
     *
     * @return
     */
    @Override
    public IndexTodayInfoVO getTodayInfo() {
        //1.获取当前登录用户
        String username = SecurityUtils.getUsername();
        //2.获取当前系统时间年月日
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        String format = sdf.format(new Date());//format=2022-12-12
        String now = DateUtils.getDate();
        //3.获取当前登录用户的今日线索
        Integer todayClueNum = reportMpper.getTodayInfo(username, now);
        //4.获取当前登陆用户今日正在跟踪的商机
        Integer todayBusinessNum = reportMpper.getTodoInfo(username, now);
        Integer customerNum = reportMpper.getbusinessChangeStatistics(username, now);
        //4.获取当前登陆用户今日交易额
        Integer totalMoney = reportMpper.getTodaySalesAmount(username, now);
        //将上面的四个值封装到对象中
        IndexTodayInfoVO index = new IndexTodayInfoVO();
        index.setTodayCluesNum(todayClueNum);
        index.setTodayBusinessNum(todayBusinessNum);
        index.setTodayContractNum(customerNum);
        index.setTodaySalesAmount((double) totalMoney);
        return index;
    }

    /**
     * 查询待处理信息
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public IndexTodoInfoVO getTodoInfo(String beginCreateTime, String endCreateTime) {

        //1.获取当前登录用户
        String username = SecurityUtils.getUsername();
        /**
         * 方式一:代码性能调优-异步并行多线程
         */
        IndexTodoInfoVO result = new IndexTodoInfoVO();
        try {
            CompletableFuture<Integer> clueNums = CompletableFuture.supplyAsync(() -> {//同步锁
                //2.1开始查询第一个属性
                return reportMpper.getTodoClueNum(username, beginCreateTime, endCreateTime);
            });
            CompletableFuture<Integer> businessNum = CompletableFuture.supplyAsync(() -> {
                //2.1开始查询第二个属性
                return reportMpper.getTodoBusinessNum(username, beginCreateTime, endCreateTime);
            });
            CompletableFuture<Integer> assignClueNum = CompletableFuture.supplyAsync(() -> {
                //2.1开始查询第三个属性
                return reportMpper.getTodoAssignClueNum(username, beginCreateTime, endCreateTime);
            });
            CompletableFuture<Integer> assignBusinessNum = CompletableFuture.supplyAsync(() -> {
                //2.1开始查询第四个属性
                return reportMpper.getTodoAssignBusinessNum(username, beginCreateTime, endCreateTime);
            });
            //3. join等待所有线程全部执行完成
            CompletableFuture.allOf(clueNums, businessNum, assignClueNum, assignBusinessNum).join();
            //4.封装结果集对象
            result.setTofollowedCluesNum(clueNums.get());
            result.setTofollowedBusinessNum(businessNum.get());
            result.setToallocatedCluesNum(assignClueNum.get());
            result.setToallocatedBusinessNum(assignBusinessNum.get());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return result;
        /**
         * 方式二.未调优
         */
     /*   //2.调用mapper,当前用户在指定时间范围内待跟进线索的数量
        Integer todoClueNum = reportMpper.getTodoClueNum(username, beginCreateTime, endCreateTime);
        //3.调用mapper,当前用户在指定时间范围内待跟进商机的数量
        Integer todoBusinessNum = reportMpper.getTodoBusinessNum(username, beginCreateTime, endCreateTime);
        //4.调用mapper,当前用户在指定时间范围内创建的线索但是是待分配处理线索的数量
        Integer todoAssignClueNum = reportMpper.getTodoAssignClueNum(username, beginCreateTime, endCreateTime);
        //4.调用mapper,当前用户在指定时间范围内创建的商机但是是待分配处理商机的数量
        Integer TodoAssignBusinessNum = reportMpper.getTodoAssignBusinessNum(username, beginCreateTime, endCreateTime);
        IndexTodoInfoVO todoInfoVO = new IndexTodoInfoVO();
        todoInfoVO.setTofollowedCluesNum(todoClueNum);
        todoInfoVO.setTofollowedBusinessNum(todoBusinessNum);
        todoInfoVO.setToallocatedCluesNum(todoAssignClueNum);
        todoInfoVO.setToallocatedBusinessNum(TodoAssignBusinessNum);
        return todoInfoVO;*/
    }


    /**
     * 学科报名统计饼图
     */
    public List<Map> subjectStatistics(String beginTime, String endTime) {
        List<Map> list = reportMpper.subjectStatistics(beginTime, endTime);
        return list;

    }

    /**
     * 统计分析--线索统计--新增线索数量折线图
     */
    @SneakyThrows
    @Override
    public LineChartVO cluesStatistics(String beginTime, String endTime) {
        LineChartVO LineChartVO = new LineChartVO();

        List<String> list = getDays(beginTime, endTime);

        //设置时间区间.(横轴)
        LineChartVO.setxAxis(list);
        LineSeriesVO seriesVO = new LineSeriesVO();
        LineSeriesVO seriesVO1 = new LineSeriesVO();
        seriesVO.setName("新增线索数量");
        seriesVO1.setName("线索总数量");

        List list1 = new ArrayList();//存放每一天新增的线索数
        List list2 = new ArrayList();//存放线索总数量


        //分组查询指定区间每天的新增数
        List<Map> map = reportMpper.cluesStatistics(beginTime, endTime);
        int sum = 0;
        for (String days : list) {
            boolean flag = true;
            for (Map map1 : map) {
                if (days.equals(map1.get("dd"))) {
                    list1.add(Integer.parseInt(map1.get("num").toString()));//存放天新增
                    sum += Integer.parseInt(map1.get("num").toString());
                    list2.add(sum);//存放总数量
                    flag = false;
                }
            }
            if (flag) {
                list1.add(0);
                list2.add(sum);
            }
        }
        seriesVO.setData(list1);
        seriesVO1.setData(list2);
        ArrayList<LineSeriesVO> lineSeriesVOList = new ArrayList<>();
        lineSeriesVOList.add(seriesVO);
        lineSeriesVOList.add(seriesVO1);
        LineChartVO.setSeries(lineSeriesVOList);
        return LineChartVO;

    }

    //用于指定时间区间同的时间集合
    private List<String> getDays(String beginCreateTime, String endCreateTime) throws ParseException {
        ArrayList<String> list = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date dBegin = dateFormat.parse(beginCreateTime);
        Date dEnd = dateFormat.parse(endCreateTime);
        Calendar calendar = Calendar.getInstance();//获取日历类
        calendar.setTime(dBegin);//设置日历的当前时间为开始时间

        list.add(beginCreateTime);//将开始时间放入list集合

        while (dEnd.after(dBegin)) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            dBegin = calendar.getTime();
            list.add(dateFormat.format(dBegin));
        }
        return list;
    }

    /**
     * 商机转换龙虎榜
     *
     * @param request
     * @return
     */
    @Override
    public List<Map<String, Object>> businessChangeStatisticsForIndex(IndexStatisticsVo request) {
        int allBusiness = businessMapper.countAllBusiness(request.getBeginCreateTime(), request.getEndCreateTime());
        List<Map<String, Object>> list = businessMapper.countAllContractByUser(request);
        for (Map<String, Object> datum : list) {
            Long num = (Long) datum.get("num");
            datum.put("radio", getRadio(allBusiness, num));
        }
        return list;
    }

    /**
     * 线索转化龙虎榜
     */
    @Override
    public List<Map<String, Object>> clueChangeStatisticsForIndex(IndexStatisticsVo request) {
        int allclues=  clueMapper.countAllClues(request.getBeginCreateTime(),request.getEndCreateTime());
        List<Map<String,Object>> list= clueMapper.countAllClueByUser(request);
        //计算转换率
        for (Map<String, Object> datum : list) {
            Long num= (Long) datum.get("num");
            datum.put("radio",getRadio(allclues,num));
        }
        return list;
    }
    /**
     * 漏斗统计
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public VulnerabilityMapVo getVulnerabilityMap(@Param("beginCreateTime") String beginCreateTime, @Param("endCreateTime") String endCreateTime) {
        VulnerabilityMapVo vulnerabilityMapDTO = new VulnerabilityMapVo();
        //线索数
        vulnerabilityMapDTO.setCluesNums(clueMapper.countAllClues(beginCreateTime, endCreateTime));
        //有效线索数
        vulnerabilityMapDTO.setEffectiveCluesNums(clueMapper.effectiveCluesNums(beginCreateTime, endCreateTime));
        //商机数
        vulnerabilityMapDTO.setBusinessNums(businessMapper.businessNumsFromClue(beginCreateTime, endCreateTime));
        //合同数
        vulnerabilityMapDTO.setContractNums(contractMapper.contractNumsFromBusiness(beginCreateTime, endCreateTime));
        return vulnerabilityMapDTO;
    }


}